package com.gp.testonline.controller;

import com.gp.testonline.eo.ClassEOPage;
import com.gp.testonline.model.Class;
import com.gp.testonline.service.ClassService;
import com.gp.testonline.tool.ResponseMessage;
import com.gp.testonline.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对外提供数据服务接口
 */
@Api(tags = "class")
@RestController
public class ClassController {
    @Autowired
    private ClassService classService;

    @ApiOperation(value = "班级添加方法")
    @RequestMapping(value = "/insertClass",method = RequestMethod.GET)
    public ResponseMessage insert(Class classes){
        int IsInsert=0;
        //传入数据的判断
        //(1) class不能为空
        if (classes!=null){
            //(2) 班级名不能为空
            if (classes.getCname()!=null){
                //(3) 添加班级数据
                IsInsert=classService.InsertClass(classes);
                if (IsInsert==1){
                    return Result.success("添加成功！");
                }else if (IsInsert==-1){
                    return Result.error("班级重复不允许添加！");
                }else{
                    return Result.error("保存失败！");
                }
            }
        }
        return Result.error("保存失败！");
    }

    @ApiOperation(value = "班级修改方法")
    @RequestMapping(value ="/updateClass",method = RequestMethod.GET)
    public ResponseMessage updateClass(Class classes){
        int isUpdate=0;
        //传入的参数不能为空
        if (classes!=null){
            //参数当中主键不能为0
            if (classes.getCid()!=0){
                isUpdate=classService.UpdateClass(classes);
                if (isUpdate==1){
                    return Result.success("更新成功！");
                }else {
                    return Result.error("更新失败！班级名重复！");
                }
            }else {
                return Result.error("传入的参数不正确！");
            }
        }
        return Result.error("传入的参数不正确！");
    }

    @ApiOperation(value = "班级删除方法")
    @RequestMapping(value = "/deleteClass",method = RequestMethod.GET)
    public ResponseMessage deleteClass(int cid){
        if (cid!=0){
            int deleteInt=0;
            deleteInt=classService.DeleteClass(cid);
            if (deleteInt>0){
                return Result.success("删除成功！");
            }else {
                return Result.error("删除失败！");
            }
        }else{
            return Result.error("删除的主键不能为0，请重新输入！");
        }
    }

    @ApiOperation(value = "根据条件获取班级详细信息")
    @RequestMapping(value = "/getClass",produces = "application/json;charest=utf-8",method = RequestMethod.PUT)
    public ResponseMessage getClass(ClassEOPage classEOPage) {
        Map<String, List> map = new HashMap<>();
        List<ClassEOPage> listClass = classService.getClassAll(classEOPage);
        map.put("list",listClass);
        return Result.success("200",map);
    }
}
